import collections


class Solution(object):
    def numOfMinutes(self, n, headID, manager, informTime):
        d = collections.defaultdict(list)
        self.max_len = 0
        for index, p in enumerate(manager):
            d[p].append(index)

        def dfs(id, time):
            if not d[id]:
                self.max_len = max(self.max_len, time)
            else:
                for v in d[id]:
                    dfs(v, time + informTime[id])

        dfs(headID, 0)
        return self.max_len